<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="home">
{
  layout: 'default',
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '首页',
  },
}
</route>
<template>
  <PageLayout :navbarShow="false" style="height:auto;">
    <!--轮播图-->
    <!-- prettier-ignore -->
    <swiper class="swiper" :indicator-dots="true" :circular="true" :autoplay="true" :interval="5000" :duration="500">
      <!--本地配置轮播图-->
      <swiper-item v-for="(item, index) in swiperList" :key="index" v-if="isLocalConfig">
        <image :src="item.url" mode="aspectFill" v-if="item.type == 'image'"></image>
        <video :src="item.url" autoplay loop muted :show-play-btn="false" :controls="false"  objectFit="cover"  v-if="item.type == 'video'"></video>
      </swiper-item>
      <!--线上配置轮播图-->
      <swiper-item v-for="(item, index) in carouselList" :key="index" v-if="!isLocalConfig">
        <image :src="getFileAccessHttpUrl(item.logo)" mode="aspectFill" @click="toPreview(index)"></image>
      </swiper-item>
    </swiper> 

      <view class="serveBox" style="background-color: #fdfdfd;padding:0 0;">  
		<wd-grid :gutter="6" clickable>
		  <wd-grid-item use-slot   v-for="(item,index) in usList"
		   @click="navBtnClick(index)"
		   style="padding:10rpx;">
		  <view use-slot  >
		   <image class="gridimg" mode="scaleToFill" :src="item.img" ></image>
			<wd-text bold size="14px" color="#1060c1" :text="item.text"></wd-text>
		  </view> 
		  </wd-grid-item>
		</wd-grid>
      </view>
	 <view    >
      <!--其他服务-->
      <view class="serveBox" style="margin-top:-12px;">
        <view class="title">
          <view class="dot"></view>
          <wd-text text="企业风采"></wd-text>
        </view>
		<uni-list> 				 
				<uni-list-item direction="row" v-for="item in companylist" :key="item.id" :title="item.name" :note="'地址：'+item.address+'\n'+item.createBy + ' '+item.createTime" :to="'/pages/main/company/companydetail?id='+item.id">
					 	<template v-slot:header>
						<view class="uni-thumb">
						 		<image :src="item.logo" mode="scaleToFill"></image>
						</view>
					</template>
				</uni-list-item>
		 </uni-list>
        <view class="title">
          <view class="dot"></view>
          <wd-text text="企业产品"></wd-text>
        </view>
        <uni-list> 				 
        		<uni-list-item direction="row" v-for="item in productlist" :key="item.id" :title="item.productname" :note="'产品型号：'+item.productspecification+  '\n产品类型：'+item.producttype_dictText" :to="'/pages/main/company/productdetail?id='+item.id">
        			 	<template v-slot:header>
        				<view class="uni-thumb">
        				 		<image :src="item.imageurl" mode="scaleToFill"></image>
        				</view>
        			</template>
        		</uni-list-item>
         </uni-list>
      </view>
    </view>
  </PageLayout>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { TestEnum } from '@/typings'
import { us, os } from '@/common/work'
// 获取当前运行平台
import PLATFORM from '@/utils/platform'
import { cache, getFileAccessHttpUrl, hasRoute } from '@/common/uitls'
import { onLaunch, onShow, onHide, onLoad, onReady } from '@dcloudio/uni-app'
import { useToast, useMessage, useNotify } from 'wot-design-uni'
import { useRouter } from '@/plugin/uni-mini-router'
import Grid from '@/components/Grid/Grid.vue'

import {
  ACCESS_TOKEN,
  USER_NAME,
  USER_INFO,
  APP_ROUTE,
  APP_CONFIG,
  HOME_CONFIG_EXPIRED_TIME,
} from '@/common/constants'
import { http } from '@/utils/http'

defineOptions({
  name: 'index',
  options: {
    // apply-shared‌：当前页面样式会影响到子组件样式.(小程序)
    // shared‌：当前页面样式影响到子组件，子组件样式也会影响到当前页面.(小程序)
    styleIsolation: 'shared',
  },
})
const toast = useToast()
const router = useRouter()
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
const isLocalConfig = getApp().globalData.isLocalConfig
const nopic=ref('/static/images/nopic.png') 
const carouselList = ref([])
const swiperList = ref([])
const middleApps = ref([])
const usList = ref([{
						itemKey: 'gfbz',
						img:'/static/qiye.png',
						text: '企业风采',
						width: '60rpx',
						height: '60rpx'
					},
					{
						itemKey: 'spfbz',
						img:'/static/fruit.png',
						text: '企业成果',
						width: '60rpx',
						height: '60rpx'
					},
					{
						itemKey: 'wzbl',
						img:'/static/news.png',
						text: '新闻资讯',
						width: '60rpx',
						height: '60rpx'
					},
					{
						itemKey: 'mxf',
						img:'/static/images/live_blue.png',
						text: '直播平台',
						width: '60rpx',
						height: '60rpx'
					}])
const osList = ref([])
const companylist=ref([])
const productlist=ref([])
const msgCount = ref(0)
const dot = ref({ mailHome: false })
const goPage = (item) => {
  let page = item.routeIndex
  console.log('-----------page------------', page)
  if (!page) {
    toast.info('该功能暂未实现')
  } else {
    if (['other', 'common'].includes(page)) {
      goPageMore(page)
      return
    }
    if (page === 'annotationList') {
      msgCount.value = 0
    }
    dot.value[page] = false
    if (page.indexOf('/app/online') == 0) {
      let code = page.substring(page.lastIndexOf('/') + 1)
      let real = { desformCode: code, desformName: item.title }
      uni.navigateTo({
        url: '/pages/check/onlineForm/add?item=' + encodeURIComponent(JSON.stringify(real)),
      })
    } else if (page.indexOf('/app/desform') == 0) {
      let code = page.substring(page.lastIndexOf('/') + 1)
      let real = { desformCode: code, desformName: item.title }
      uni.navigateTo({
        url: '/pages/check/designForm/designForm?item=' + encodeURIComponent(JSON.stringify(real)),
      })
    }else {
      if (!hasRoute({ name: page })) {
        router.push({ name: 'demo', params: { backRouteName: 'index' } })
      } else {
        router.push({ name: page, params: { backRouteName: 'index' } })
      }
    }
  }
}
const getAppConfigRoute = () => {
  //判断是否过期
  let config = cache(APP_CONFIG)
  if (config) {
    homeConfig()
  } else {
    //更新首页配置
    // http.get('/eoa/sysAppConfig/queryAppConfigRoute').then((res: any) => {
    //   console.log('更新首页配置res', res)
    //   let result = res
    //   if (result.success) {
    //     cache(APP_ROUTE, result.result.route, HOME_CONFIG_EXPIRED_TIME)
    //     cache(APP_CONFIG, result.result.config, HOME_CONFIG_EXPIRED_TIME)
    //     homeConfig()
    //   }
    // })
  }
}
const homeConfig = () => {
  var indexRouteList = cache(APP_ROUTE)
  var appConfig = cache(APP_CONFIG)
  //usList.value = indexRouteList.filter((item) => item.type == 'common')
  //osList.value = indexRouteList.filter((item) => item.type == 'other')
  //middleApps.value = indexRouteList.filter((item) => item.type == 'approve')
  // let carouselImgStr = appConfig[0].carouselImgJson
  // var carouselImgArr = carouselImgStr && carouselImgStr.length > 0 ? carouselImgStr.split(',') : []
  // carouselList.value = carouselImgArr
}
const goPageMore = (page) => {
  // router.replace({ path: `/pages/more/more`, query: { type: page } })
  router.push({ name: 'more', params: { backRouteName: 'index', type: page } })
}
const getSwiperList=()=>{
	 
	if(getApp().globalData.swiperList.length==0){
		http.get("/api/slider/list").then(res=>{
			 
			console.log(res)
				for(var i=0;i<res.result.length;i++){ 
					if(res.result[i].logo){
						var pics=res.result[i].logo.split(",")
						res.result[i].logo=getApp().globalData.imgUrl+pics[0]
						carouselList.value.push(res.result[i])
					}
					
				}
				getApp().globalData.swiperList=carouselList.value;  
		})
	}else{
		carouselList.value=getApp().globalData.swiperList
	}
}
const toPreview=(index)=>{ 
	var linkType= carouselList.value[index].type;
	var linkId= carouselList.value[index].id;
	//1  企業  2  論文   3  產品   4  專利
	console.log(linkType+","+linkId)
	if(linkType==1){
			 uni.navigateTo({
				url:'/pages/main/company/companydetail?id='+linkId
			 })
	}
	if(linkType==2){
		 uni.navigateTo({
			url:'/pages/main/company/paperdetail?id='+linkId
		 })
	}
	if(linkType==3){
			uni.navigateTo({
				url:'/pages/main/company/productdetail?id='+linkId
			})				 
	}
	if(linkType==4){
			uni.navigateTo({
				url:'/pages/main/company/patentdetail?id='+linkId
			})  
	}
	// uni.previewImage({
	// 	urls:getApp().globalData.swiperList,
	// 	current:index
	// })
}
const getCompanyList=()=>{
				 
				http.get("/api/company/list").then(res=>{
				 
				 for(var i=0;i<res.result.records.length;i++){
					 if(res.result.records[i].logo)
					 res.result.records[i].logo=getApp().globalData.imgUrl+res.result.records[i].logo
					 else
					 res.result.records[i].logo= nopic.value
				 }
				  companylist.value=res.result.records
				 
			 })
			} 
const getProductList=()=>{
				uni.showLoading({
					title:"加载数据中..."
				})
				http.get("/api/product/list").then(res=>{
				 uni.hideLoading() 
				 for(var i=0;i<res.result.records.length;i++){
					 if(res.result.records[i].imageurl){
						 var str1=res.result.records[i].imageurl.split(",")
						res.result.records[i].imageurl=getApp().globalData.imgUrl+str1[0]
						 
					 }
					  else
					 res.result.records[i].imageurl= nopic.value
				 }
				  productlist.value=res.result.records
				 
			 })
			} 
onLoad(() => {
  console.log('index页面：onLoad')
})
onReady(() => {
  console.log('index页面：onReady')
  getSwiperList();
  getCompanyList();
  getProductList();
})
const navBtnClick=(index)=>{
	console.log(index)
	if(index==0){
		uni.navigateTo({
			url:'/pages/main/company/companylist'
		})
	}
	if(index==1){
		uni.navigateTo({
			url:'/pages/main/company/companyfruit'
		})
	}
	if(index==2){
		uni.navigateTo({
			url:'/pages/main/article/articlelist'
		})
	}
	if(index==3){
		uni.switchTab({
			url:'/pages/live/index'
		})
	}
}

if (isLocalConfig) {
  // usList.value = us.data.map((item) => ({
  //   ...item,
  //   text: item.title,
  //   img: item.icon,
  //   itemKey: item.routeIndex,
  // }))
  // osList.value = os.data.map((item) => ({
  //   ...item,
  //   text: item.title,
  //   img: item.icon,
  //   itemKey: item.routeIndex,
  // }))
  // usList.value.push({
  //   text: '更多',
  //   img: '/static/index/128/more.png',
  //   routeIndex: 'other',
  //   itemKey: 'other',
  // })
  // osList.value.push({
  //   text: '更多',
  //   img: '/static/index/128/more.png',
  //   routeIndex: 'common',
  //   itemKey: 'common',
  // })
  middleApps.value = [
    {
      icon: 'https://static.jeecg.com/upload/test/line2_icon1_1595818065964.png',
      title: '审批',
      subTitle: '个人审批',
      routeIndex: 'paper',
    },
    {
      icon: 'https://static.jeecg.com/upload/test/line2_icon2_1595818070168.png',
      title: '审批稿',
      subTitle: '审批草稿箱',
      routeIndex: 'draft',
    },
  ]
  swiperList.value = [
    {
      id: 1,
      type: 'image',
      url: 'https://static.jeecg.com/upload/test/banner0_1595850438042.jpeg',
      link: '',
    },
    {
      id: 2,
      type: 'image',
      url: 'https://static.jeecg.com/upload/test/banner2_1595818081327.jpg',
      link: '',
    },
    {
      id: 3,
      type: 'image',
      url: 'https://static.jeecg.com/upload/test/oabanner-2_1595648520760.png',
      link: '',
    },
    {
      id: 4,
      type: 'image',
      url: 'https://static.jeecg.com/upload/test/banner5_1595818089013.jpeg',
      link: '',
    },
  ]
} else {
  getAppConfigRoute()
}
</script>

<style lang="scss" scoped>
	@import '@/common/uni-ui.scss';
.swiper {
  height: 375upx;
  flex: none;
  image,
  video {
    width: 100%;
    display: block;
    height: 100%;
    margin: 0;
  }
  :deep(.uni-swiper-dot) {
    transition: all 400ms ease;
    background-color: rgba(255, 255, 255, 0.4);
    width: 5px;
    height: 5px;
    border-radius: 50%;
    margin: 0 4px;
  }
  :deep(.uni-swiper-dot-active) {
    background-color: rgba(255, 255, 255, 1);
    width: 16px;
    border-radius: 2px;
  }
}
  
  .serveBox {
    margin-bottom: 0upx;
    background-color: #fff;
	border-radius: 15upx;
	border:1px solid #eeecec;
    &:last-child {
      .title {
        .dot {
          background-color: #3b78f1;
		  height:20upx;
        }
      }
    }
    :deep(.wd-grid-item) {
      &:not(.enabled) {
        // filter: grayscale(1);
      }
    }
    .title {
      display: flex;
      align-items: center;
      padding-left: 30upx;
      height: 48px;
      .dot {
        width: 14upx;
        height: 14upx;
        background-color: #3b78f1;
        border-radius: 100%;
        margin-right: 20upx;
      }
      .wd-text {
        color: #666;
        font-size: 15px;
      }
    }
  }
 
  .gridimg {
	  padding:25rpx;border-radius:25rpx;
	  height:22vw;
	  border:1px solid #f9fdff;
	  background-image: linear-gradient(  #ffafb1 ,#efe9e9);
	  height: 80px !important;   
  }
  
 .list-picture {
 	width: 100%;
 	height: 145px;
 }
 
 .thumb-image {
 	width: 100%;
 	height: 100%;
 }
 
 .ellipsis {
 	display: flex;
 	overflow: hidden;
 }
 
 .uni-ellipsis-1 {
 	overflow: hidden;
 	white-space: nowrap;
 	text-overflow: ellipsis;
 }
 
 .uni-ellipsis-2 {
 	overflow: hidden;
 	text-overflow: ellipsis;
 	display: -webkit-box;
 	-webkit-line-clamp: 2;
 	-webkit-box-orient: vertical;
 }
 
</style>
